package org.jeecg.modules.saledetail.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.saledetail.entity.BoxSaledetail;
import org.jeecg.modules.saledetail.service.IBoxSaledetailService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 分销系统佣金
 * @Author: jeecg-boot
 * @Date:   2023-05-27
 * @Version: V1.0
 */
@Api(tags="分销系统佣金")
@RestController
@RequestMapping("/saledetail/boxSaledetail")
@Slf4j
public class BoxSaledetailController extends JeecgController<BoxSaledetail, IBoxSaledetailService> {
	@Autowired
	private IBoxSaledetailService boxSaledetailService;
	
	/**
	 * 分页列表查询
	 *
	 * @param boxSaledetail
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "分销系统佣金-分页列表查询")
	@ApiOperation(value="分销系统佣金-分页列表查询", notes="分销系统佣金-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BoxSaledetail>> queryPageList(BoxSaledetail boxSaledetail,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BoxSaledetail> queryWrapper = QueryGenerator.initQueryWrapper(boxSaledetail, req.getParameterMap());
		Page<BoxSaledetail> page = new Page<BoxSaledetail>(pageNo, pageSize);
		IPage<BoxSaledetail> pageList = boxSaledetailService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param boxSaledetail
	 * @return
	 */
	@AutoLog(value = "分销系统佣金-添加")
	@ApiOperation(value="分销系统佣金-添加", notes="分销系统佣金-添加")
	@RequiresPermissions("saledetail:box_saledetail:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BoxSaledetail boxSaledetail) {
		boxSaledetailService.save(boxSaledetail);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param boxSaledetail
	 * @return
	 */
	@AutoLog(value = "分销系统佣金-编辑")
	@ApiOperation(value="分销系统佣金-编辑", notes="分销系统佣金-编辑")
	@RequiresPermissions("saledetail:box_saledetail:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BoxSaledetail boxSaledetail) {
		boxSaledetailService.updateById(boxSaledetail);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "分销系统佣金-通过id删除")
	@ApiOperation(value="分销系统佣金-通过id删除", notes="分销系统佣金-通过id删除")
	@RequiresPermissions("saledetail:box_saledetail:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		boxSaledetailService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "分销系统佣金-批量删除")
	@ApiOperation(value="分销系统佣金-批量删除", notes="分销系统佣金-批量删除")
	@RequiresPermissions("saledetail:box_saledetail:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.boxSaledetailService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "分销系统佣金-通过id查询")
	@ApiOperation(value="分销系统佣金-通过id查询", notes="分销系统佣金-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BoxSaledetail> queryById(@RequestParam(name="id",required=true) String id) {
		BoxSaledetail boxSaledetail = boxSaledetailService.getById(id);
		if(boxSaledetail==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(boxSaledetail);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param boxSaledetail
    */
    @RequiresPermissions("saledetail:box_saledetail:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BoxSaledetail boxSaledetail) {
        return super.exportXls(request, boxSaledetail, BoxSaledetail.class, "分销系统佣金");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("saledetail:box_saledetail:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BoxSaledetail.class);
    }

	 public static void main(String[] args) {
		 //创建卡牌库
		 String [] card={"B:小乔","B:大乔","S:吕布","A:赵云","A:张飞","A:周瑜"
				 ,"B:大乔","B:关羽","A:许褚","A:张郃","B:刘备","B:关羽","B:黄忠",
				 "B:颜良","A:张辽", "S:曹操", "B:刘备","B:小乔","B:黄忠","B:马岱"};
		 //产生随机数
		 Random rd=new Random();
		 //初始化随机账户金额
		 int count=rd.nextInt(2000)+1880;
		 //随机数初始化
		 int num=0;
		 //创建扫描器
		 Scanner sc=new Scanner(System.in);

		 //循环抽奖
		 while(count>=188){
			 //选择抽奖方式
			 System.out.println("请选择单抽还是连抽:\n1.单抽\t2.10连抽");
			 //接收输入数字
			 int numInput=sc.nextInt();
			 if(numInput==1){
				 num=rd.nextInt(card.length-1);
				 System.out.print(card[num]+"\t\n");
				 count-=188;
				 System.out.println("您的余额为："+count);
			 }else if(numInput==2 ){
				 if(count>=1880){
					 for(int i=1;i<=10;i++){
						 num=rd.nextInt(card.length-1);
						 System.out.print(card[num]+"\t");
					 }
					 System.out.println();
					 count-=1880;
					 System.out.println("你的余额为："+count);
				 }
				 else{
					 System.out.println("您的余额不足，请充值或者选择单抽");
				 }
			 }else{
				 System.out.print("输入的抽奖序号不正确，请重新输入:");
			 }
		 }
	 }

}
